$(function(){
	$('#tabs').tabs();
});

var diapason = 0;
var longVib = 0;
var j = 0;
var nbreFrets = 0;
var t = 0;
	
	/*----------------------------Init purpose-------------------------*/
	if(!localStorage.decimalPrecision){
		localStorage.decimalPrecision = 1000;
	}
	
	if (!localStorage.gmbmid){
		chrome.bookmarks.create({
			'parentId': '1',
			'title': 'Guitar Maker bookmarks'},
			function(newFolder) {
				console.log("added folder: " + newFolder.id);
				localStorage.gmbmid = newFolder.id;
		});
	}
	foo();
	//constructBTNList();
	/*---------------------------END------------------------------------*/

function constructBTNList(){
	$("#BTNList").empty();
	chrome.bookmarks.getChildren(localStorage.gmbmid,
		function(BTN){
			BTNnbre = BTN.length;
			var urlToOpen;
			var title;
			$("#BTNList").append("<ul>");
			for(i = 0; i < BTN.length; i++){
				urlToOpen = BTN[i].url;
				title = BTN[i].title.substring(0,55);
				if(BTN[i].title.length > 55){
					title += "...";
				}
				$("#BTNList").append("<li style='list-style:none;  line-height: 20px;'><img style='vertical-align: middle;' src='chrome://favicon/" + BTN[i].url + "' \> <a href=\"#\" onclick=\"javascript:openURL('"+ urlToOpen +"');\" alt='"+ BTN[i].url +"'>" + title + '</a></li>');
			}
			$("#BTNList").append("</ul>");
		}
	);
}	

function openURL(urlToOpen){
	chrome.tabs.create({url: urlToOpen});
}

function foo(){
	init();
	$("#affichage").append('<table><tr>');
	$("#affichage").append('<th>Fret</th>');
	$("#affichage").append('<th>form nut</th>');
	$("#affichage").append('<th>fret to fret</th></tr>');
	for(i=1;i<=nbreFrets;i++){
		longVib = calcul(longVib);
		$("#affichage").append('<tr>');
		$("#affichage").append('<td>' + i + '</td>');
		$("#affichage").append('<td>' + arrondir((diapason - longVib)) + '</td>');
		$("#affichage").append('<td>' + arrondir(((diapason - longVib) - j)) + '</td>');
		$("#affichage").append('</tr>');
		j = (diapason - longVib);
	}
	$("#affichage").append('</table>');
}

function format(){
	formata = document.getElementById("preformat");
	document.getElementById("diapas").value = formata.options[formata.selectedIndex].value;
}

function init(){
	document.getElementById("affichage").innerHTML = "";
	diapason = document.getElementById("diapas").value;
	longVib = document.getElementById("diapas").value;
	nbreFrets = document.getElementById("nbreFret").value;
	j = 0;
}

function calcul(res){
	res = res / Math.pow(2, 1/12);
	return res;
}
function arrondir(resultat) {
      resultat = Math.round(resultat*localStorage.decimalPrecision)/localStorage.decimalPrecision;
      return resultat;
}

function fooNA(){
	var cordierM = parseInt(document.getElementById("cordier_max").value);
	var cordierm = parseInt(document.getElementById("cordier_min").value);
	var nbrFret = parseInt(document.getElementById("nbreFretNA").value);
	var finger = parseInt(document.getElementById("fingerNA").value);
	var SL = parseInt(document.getElementById("diapasNA").value);
	
	t = SL;
	for(i=1;i<=nbrFret;i++){
		t = calcul(t);
	}
	
	var a = ((cordierM + cordierm) / 2) - (finger + 2); // fingerboard height + action height - average tailpiece height
	var b = t; // lenght between tail piece and point above last fret
	var result = arrondir((Math.asin(a/b)) * 180 / Math.PI);
	document.getElementById("affichageNA").innerHTML = "<strong>" + result + " (deg)</strong>";
}

function addBM(){
	chrome.tabs.getSelected(null,
		function(tab){
			chrome.bookmarks.create({
				'parentId': localStorage.gmbmid,
				'title': tab.title,
				'url': tab.url
			},function(newBTN){ 
				$("#BTNList").append("<li style='list-style:none;  line-height: 20px;'><img style='vertical-align: middle;' src='chrome://favicon/" + newBTN.url + "' \> <a href=\"#\" onclick=\"javascript:openURL('"+ newBTN.url +"');\" alt='"+ newBTN.url +"'>" + newBTN.title + '</a></li>');
			});
	});
}